package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import javax.management.Query;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.QualitySapPurchaseOrder;
import cc.comac.mom.bean.base.LimitPageBean;
import cc.comac.mom.dao.entity.QualitySapPurchaseOrderDao;
import cc.comac.mom.entity.QualityQarCheckInvoiceEntity;
import cc.comac.mom.entity.QualitySapPurchaseOrderEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;

@Component
@Transactional
public class QualitySapPurchaseOrderDataHandler extends ServiceImpl<QualitySapPurchaseOrderDao, QualitySapPurchaseOrderEntity>{

	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	
	@Autowired
	private QualitySapPurchaseOrderDao qualitySapPurchaseOrderDao;
	
	//新增SAP入库单
	public ResultBean insert(QualitySapPurchaseOrder obj) {
		ResultBean resultBean = new ResultBean(true);
		if(obj == null || obj.getInnerData() == null) {
			resultBean.setErrorMessge("数据为空");
			return resultBean;
		}
		baseEntityDataUtil.setBaseEntityData(obj.getInnerData());
		int insert = qualitySapPurchaseOrderDao.insert(obj.getInnerData());
		if(insert<1) {
			resultBean.setErrorMessge("保存SAP采购单信息失败");
			return resultBean;
		}
		return resultBean;
	}
	//批量新增采购订单
	
	public ResultBean insertBatch (List<QualitySapPurchaseOrder> list) {
		ResultBean resultBean = new ResultBean(true);
		if(list == null || list.size()<1) {
			resultBean.setErrorMessge("数据为空");
			return resultBean;
		}
		ArrayList<QualitySapPurchaseOrderEntity> listEntity = new ArrayList<QualitySapPurchaseOrderEntity>();
		for(QualitySapPurchaseOrder item :list) {
			QualitySapPurchaseOrderEntity entity=item.getInnerData();
			if(entity == null) {
				resultBean.setErrorMessge("数据为空");
				return resultBean;
			}
			baseEntityDataUtil.setBaseEntityData(entity);
			listEntity.add(entity);
		
		}
		boolean saveBatch = this.saveBatch(listEntity);
		if(!saveBatch) {
			resultBean.setErrorMessge("入库单新增失败");
			return resultBean;
		}
		
		return resultBean;
	}

	public ResultBean selectBySomeField(QualitySapPurchaseOrder qualitySapPurchaseOrder) {
		ResultBean resultBean = new ResultBean(true);
		if (qualitySapPurchaseOrder.getPageNum() == 0) {
			qualitySapPurchaseOrder.setPageNum(1);
		}
		if (qualitySapPurchaseOrder.getPageSize() == 0) {
			qualitySapPurchaseOrder.setPageSize(10);
		}
		Page<QualitySapPurchaseOrderEntity> page = new Page<QualitySapPurchaseOrderEntity>(qualitySapPurchaseOrder.getPageNum(),qualitySapPurchaseOrder.getPageSize());
		QueryWrapper<QualitySapPurchaseOrderEntity> queryWrapper = new QueryWrapper<QualitySapPurchaseOrderEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.orderByDesc("creation_time");
		IPage<QualitySapPurchaseOrderEntity> selectPage = qualitySapPurchaseOrderDao.selectPage(page, queryWrapper);
		LimitPageBean<QualitySapPurchaseOrderEntity> of = (LimitPageBean<QualitySapPurchaseOrderEntity>) LimitPageBean.of(selectPage);
		resultBean.setObject(of);
		return resultBean;
		
	}
	
	public QualitySapPurchaseOrderEntity selectBySome(String material,String paiHao,String serialNumber) {
		QueryWrapper<QualitySapPurchaseOrderEntity> queryWrapper = new QueryWrapper<QualitySapPurchaseOrderEntity>();
		queryWrapper.eq("material", material);
		queryWrapper.eq("pai_hao", paiHao);
		if (StringUtils.isNoneEmpty(serialNumber)) {
			queryWrapper.eq("serial_number", serialNumber);
		}
		QualitySapPurchaseOrderEntity selectOne2 = qualitySapPurchaseOrderDao.selectOne(queryWrapper);
		return selectOne2;
	
	}
}
